package com.yphone.net.helper;

import com.yphone.net.inf.IAnalysisJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public abstract class OkHttpClientHelper {
    private String TAG = "OkHttpClientHelper";

    public abstract void onErrorResponse(Exception exc);

    public abstract void onResponse(Object obj);

    public void sendGetRequest(String baseurl, String url, final IAnalysisJson analysisJson, int timeOut) {
        try {
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            OkHttpClientMethod.getInstance().getAsyncMethod(url, null, headers, new OkHttpClientMethod.Callback2() {
                public void onFailure(Call call, IOException e) {
                    OkHttpClientHelper.this.onErrorResponse(e);
                }

                public void onResponse(Call call, byte[] response) {
                    OkHttpClientHelper.this.onResponse(analysisJson.getData(new String(response)));
                }
            });
        } catch (Exception e) {
            onErrorResponse(e);
        }
    }

    public void sendPostRequest(String baseurl, String url, Map<String, String> map, final IAnalysisJson analysisJson, int timeOut) {
        try {
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            OkHttpClientMethod.getInstance().postAsyncMethod(url, null, map, headers, new OkHttpClientMethod.Callback2() {
                public void onFailure(Call call, IOException e) {
                    OkHttpClientHelper.this.onErrorResponse(e);
                }

                public void onResponse(Call call, byte[] response) {
                    OkHttpClientHelper.this.onResponse(analysisJson.getData(new String(response)));
                }
            });
        } catch (Exception e) {
            onErrorResponse(e);
        }
    }

    public void uploadResourcePost(String baseurl, String url, String path, Map<String, String> map, final IAnalysisJson analysisJson, int timeOut) {
        map.put("img", path);
        try {
            String str = url;
            Map<String, String> map2 = map;
            OkHttpClientMethod.getInstance().postAsyncMethod(str, null, map2, new HashMap<String, String>(), new OkHttpClientMethod.Callback2() {
                public void onFailure(Call call, IOException e) {
                    OkHttpClientHelper.this.onErrorResponse(e);
                }

                public void onResponse(Call call, byte[] response) {
                    OkHttpClientHelper.this.onResponse(analysisJson.getData(new String(response)));
                }
            });
        } catch (Exception e) {
            onErrorResponse(e);
        }
    }

    public void dispose() {
    }

    public boolean isDisposed() {
        return true;
    }
}